<template>
  <transition name="modal-fade">
    <div v-if="modelValue" class="apple-modal-overlay" @click.self="handleOverlayClick">
      <div class="apple-modal-container" :class="sizeClass">
        <div class="apple-modal-header">
          <slot name="header">
            <h2 class="modal-title">{{ title }}</h2>
          </slot>
          <button class="modal-close-btn" @click="handleClose">
            <el-icon><Close /></el-icon>
          </button>
        </div>
        
        <div class="apple-modal-body">
          <slot></slot>
        </div>
        
        <div v-if="$slots.footer" class="apple-modal-footer">
          <slot name="footer"></slot>
        </div>
      </div>
    </div>
  </transition>
</template>

<script setup lang="ts">
import { computed } from 'vue';
import { Close } from '@element-plus/icons-vue';

interface Props {
  modelValue: boolean;
  title?: string;
  size?: 'small' | 'medium' | 'large';
  closeOnOverlay?: boolean;
}

const props = withDefaults(defineProps<Props>(), {
  title: '',
  size: 'medium',
  closeOnOverlay: true
});

const emit = defineEmits<{
  'update:modelValue': [value: boolean];
  'close': [];
}>();

const sizeClass = computed(() => {
  return `modal-${props.size}`;
});

const handleClose = () => {
  emit('update:modelValue', false);
  emit('close');
};

const handleOverlayClick = () => {
  if (props.closeOnOverlay) {
    handleClose();
  }
};
</script>

<style scoped>
/* 模态框过渡动画 */
.modal-fade-enter-active,
.modal-fade-leave-active {
  transition: all 0.3s ease;
}

.modal-fade-enter-from,
.modal-fade-leave-to {
  opacity: 0;
}

.modal-fade-enter-active .apple-modal-container,
.modal-fade-leave-active .apple-modal-container {
  transition: all 0.3s ease;
}

.modal-fade-enter-from .apple-modal-container,
.modal-fade-leave-to .apple-modal-container {
  transform: scale(0.9) translateY(20px);
  opacity: 0;
}

/* 模态框遮罩层 */
.apple-modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(5px);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 99999;
  padding: 20px;
  box-sizing: border-box;
}

/* 模态框容器 */
.apple-modal-container {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border-radius: 20px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
  border: 1px solid rgba(255, 255, 255, 0.2);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  max-height: 90vh;
  width: 100%;
  animation: modalShow 0.3s ease-out;
}

@keyframes modalShow {
  from {
    opacity: 0;
    transform: scale(0.9) translateY(20px);
  }
  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

/* 模态框尺寸 */
.modal-small {
  max-width: 400px;
}

.modal-medium {
  max-width: 500px;
}

.modal-large {
  max-width: 700px;
}

/* 模态框头部 */
.apple-modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 24px 24px 0;
  position: relative;
}

.modal-title {
  font-size: 22px;
  font-weight: 600;
  color: #1d1d1f;
  margin: 0;
  letter-spacing: -0.5px;
}

.modal-close-btn {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.05);
  border: none;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  transition: all 0.2s ease;
  color: #86868b;
}

.modal-close-btn:hover {
  background: rgba(0, 0, 0, 0.1);
  transform: scale(1.05);
}

.modal-close-btn:active {
  transform: scale(0.95);
}

/* 模态框主体 */
.apple-modal-body {
  padding: 24px;
  overflow-y: auto;
  flex: 1;
}

/* 模态框底部 */
.apple-modal-footer {
  padding: 0 24px 24px;
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

/* 移动端适配 */
@media (max-width: 768px) {
  .apple-modal-overlay {
    padding: 15px;
    align-items: flex-end;
  }
  
  .apple-modal-container {
    width: 100%;
    max-height: 85vh;
    border-radius: 20px 20px 0 0;
    animation: slideUp 0.3s ease-out;
  }
  
  @keyframes slideUp {
    from {
      transform: translateY(100%);
    }
    to {
      transform: translateY(0);
    }
  }
  
  .apple-modal-header {
    padding: 20px 20px 0;
  }
  
  .modal-title {
    font-size: 20px;
  }
  
  .apple-modal-body {
    padding: 20px;
  }
  
  .apple-modal-footer {
    padding: 0 20px 20px;
  }
}

@media (max-width: 480px) {
  .apple-modal-overlay {
    padding: 10px;
  }
  
  .apple-modal-header {
    padding: 15px 15px 0;
  }
  
  .modal-title {
    font-size: 18px;
  }
  
  .apple-modal-body {
    padding: 15px;
  }
  
  .apple-modal-footer {
    padding: 0 15px 15px;
  }
}
</style>